<template>
  <div>
    <el-upload
      :disabled="disabled"
      :file-list="internalFileList"
      :show-file-list="showFile"
      :limit="maxCount"
      :on-exceed="handleExceed"
      :on-remove="handleRemove"
      :on-preview="handlePreview"
      :http-request="doUpload"
      :multiple="true"
      :before-upload="beforeUpload"
      :list-type="listType"
      accept="image/png,image/jpeg"
    >
      <i class="el-icon-plus"></i>
      <el-button slot="trigger" size="small" type="primary">选择图片</el-button>
      <div slot="tip" class="el-upload__tip">仅支持 JPG/PNG，且不超过 10MB</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt />
      <div v-if="dialogImageName" style="margin-top:8px;color:#606266;word-break:break-all;">{{ dialogImageName }}</div>
    </el-dialog>
  </div>
</template>

<script>
import { policy as uploadImage } from '@/api/oss/policy'

export default {
  name: 'multiUpload',
  props: {
    value: Array,
    maxCount: {
      type: Number,
      default: 30
    },
    listType: {
      type: String,
      default: 'picture-card'
    },
    showFile: {
      type: Boolean,
      default: true
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: false,
      dialogImageUrl: null,
      dialogImageName: null,
      internalFileList: []
    }
  },
  watch: {
    value: {
      immediate: true,
      handler(newVal) {
        const list = []
        if (Array.isArray(newVal)) {
          for (let i = 0; i < newVal.length; i++) {
            list.push({ url: newVal[i], name: newVal[i] })
          }
        }
        this.internalFileList = list
      }
    }
  },
  methods: {
    emitInput(fileList) {
      const value = []
      for (let i = 0; i < fileList.length; i++) {
        value.push(fileList[i].url)
      }
      this.$emit('input', value)
    },
    handleRemove(file, fileList) {
      this.emitInput(fileList)
    },
    handlePreview(file) {
      this.dialogVisible = true
      this.dialogImageUrl = file.url
      this.dialogImageName = file.name || (file.url && file.url.split('/').pop())
    },
    beforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!isJpgOrPng) {
        this.$message.error('仅支持 JPG/PNG 格式')
        return false
      }
      const isLt10M = file.size / 1024 / 1024 < 10
      if (!isLt10M) {
        this.$message.error('图片大小不能超过 10MB')
        return false
      }
      if (this.internalFileList.length >= this.maxCount) {
        this.$message.warning('最多只能上传' + this.maxCount + '张图片')
        return false
      }
      return true
    },
    async doUpload(request) {
      try {
        const formData = new FormData()
        formData.append('image', request.file)
        const resp = await uploadImage(formData)
        const url = resp.data || resp.msg
        this.internalFileList.push({ name: request.file.name, url })
        this.emitInput(this.internalFileList)
        request.onSuccess && request.onSuccess({}, request.file)
      } catch (e) {
        request.onError && request.onError(e)
      }
    },
    handleExceed() {
      this.$message({ message: '最多只能上传' + this.maxCount + '张图片', type: 'warning', duration: 1000 })
    }
  }
}
</script>

<style>
</style>


